local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHOLYBUIBase());
local CommonOHOH =_G.CommonOHOH
local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();
local BtnHelper = ClientOHOH.load.BtnHelper();
local LanguageHelper=_G.LanguageHelper 
local TweenHelper = ClientOHOH.load.TweenHelper();

function InitInsts(ui)
	ui.insts = {};
	ui.insts.root ="ctrl.root"

	ui.insts.close ="ctrl.close"
	-- ui.insts.title ="Frame.Frame.title"
	ui.insts.settingCell = "ctrl.cell"
	ui.insts.input = "ctrl.input"
end

function M.MobileSize(self)
	if _G.client.IsMobile then --是移动设备
		if self.insts.root then
			local pos= self.insts.root:GetAttribute("Position")
			local size=self.insts.root:GetAttribute("Size")
			if pos then
				self.insts.root.Position=pos
			end
			if size then
				self.insts.root.Size=size
			end
		end
	end
end

function M.new(...)
	return M(...);
end;
function M.ctor(self)
	InitInsts(self)

	self.RollHideEvent=ConnectClass.new();

	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.SettingGui"),2);
	self:MobileSize()

	CallBackManager:AddRbxConnect(self,self.insts.close.Activated, function()
		self:Hide()
	end);
	CallBackManager:AddRbxConnect(self,self.insts.input.Focused, function()
		self.insts.input:GetPropertyChangedSignal("Text"):Connect(function()
			--local searchTxt =string.lower(self.ui.input.Text)
			self.insts.input.Text = self.insts.input.Text:gsub("%D", "")
			--self:SearchTxtChange( self.insts.input.Text )
		end)
	 end);
	 self.insts.input.FocusLost:Connect(function(enterPressed, inputThatCausedFocusLost)
		--print("输入框",enterPressed,  inputThatCausedFocusLost)
		if enterPressed then
			--print("用户按下了 Enter 键离开了输入框")
		else
			--print("用户点击了其他地方离开了输入框")
		end
		-- 检查导致焦点丢失的输入类型
		--print("导致焦点丢失的输入类型:", inputThatCausedFocusLost.KeyCode)
		self:HideTxTSubmit( self.insts.input.Text )
	end)
	self:Hide()
end;

function M.dtor(self)
	M.super.dtor(self);
end;

function M.InitAllButtons(self)
	self:AddProcess(function()
		for _, v in pairs(self.main_inst:GetDescendants()) do
			if v:IsA("GuiButton") then
				BtnHelper:InitButton(v, nil, nil, nil, function() end);
			end;
		end;
	end);
end

function M.CreateCell(self)
	local cell=nil
	if self.usedCell then
		cell= self.insts.settingCell:Clone()
		cell.Parent=self.insts.settingCell.Parent
	else
		cell=self.insts.settingCell
		self.usedCell=true
	end
	return cell
end

function M.AddSetBtn(self, btnKey,name, state)
	assert(btnKey, "str is nil");
	local cell = self:CreateCell()
	if not cell then
		warn("\230\178\161\230\156\137\230\139\191\229\136\176UI,\229\133\139\233\154\134\230\138\165\233\148\153\239\188\129");
	end;
	cell.Txt.Text =name
	cell.Name=btnKey
	self:SetBtnSwitch2(cell.Btn, state);
	return cell.Btn;
end;

function M.SetBtnSwitch2(self, btn, state)
	if state then
		btn.text.Text=LanguageHelper:GetText(126)
		btn.BackgroundColor3=Color3.fromHex("#42cf38")
	else
		btn.text.Text=LanguageHelper:GetText(127)
		btn.BackgroundColor3=Color3.fromHex("#d55d5f")
	end
end

function M.SetBtnSwitch(self, btn, state)
	local icon = nil;
	assert(btn, "btn is nil");
	icon = btn.Icon;
	if state then
		--btn.Image = "rbxassetid://8985172014";
		btn.Icon.BackgroundColor3=Color3.new(0.321569, 1, 0.133333) --打开状态颜色
		TweenHelper:PlayTween(icon, TweenInfo.new(0.5, 1, 1), {
			Position = UDim2.new(0.57, 0, 0.5, 0) --打开状态位置
		});
		return;
	end;
	--btn.Image = "rbxassetid://8985438661";
	btn.Icon.BackgroundColor3=Color3.new(0.635294, 0.635294, 0.631373) --关闭状态颜色
	TweenHelper:PlayTween(icon, TweenInfo.new(0.5, 1, 1), {
		Position = UDim2.new(0.03, 0, 0.5, 0) --关闭状态位置
	});
end;
function M.HideTxTSubmit(self,txt)
	self.insts.input.Text = txt
	local num = tonumber(txt)
	if not num then
		num = 0
		self.insts.input.Text = "0"
	end
	
	self:AddProcess(function()
		self.RollHideEvent:Action(num)
		ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.roll_hide,num)

	end)
end

return M
